package util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Display;
/**
 * 读属性文件
 * @author zx
 *
 */
public class ReadPropertyUtil {
	/**
	 * 程序根目录
	 */
	private static String platPath ="";// Platform.getInstallLocation().getURL().getPath();
	
	public static void initPlatPath(){
		Display.getDefault().syncExec(new Runnable() {
			public void run() {
				platPath = Platform.getInstallLocation().getURL().getPath();
			}
		});
	}   
	/**
	 * 写属性文件
	 * @param filePath
	 * @param filedNname
	 * @param value
	 * @param isUtterly
	 */
	public static void writeValueByFiledName(String filePath,String filedNname,String value,boolean isUtterly){
		if(filedNname==null||"".equals(filedNname)||filePath==null||"".equals(filePath))
			return ;
		if(!isUtterly){
//			platPath = Platform.getInstallLocation().getURL().getPath();
			initPlatPath();
			filePath = platPath+filePath;
		}
		// 存储新输入的URL
		try {
			InputStream is = new FileInputStream(filePath);
			Properties props = new Properties();
			props.load(is);
			is.close();
			OutputStream os = new FileOutputStream(filePath);
			props.setProperty(filedNname, value); //$NON-NLS-1$
			props.store(os, "APPLICATION INI"); //$NON-NLS-1$
			os.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * 根据Map写属性文件
	 * @param filePath
	 * @param filedNname
	 * @param value
	 * @param isUtterly
	 */
	public static void writeValuesByMap(String filePath,HashMap<String, String> propertiesMap,boolean isUtterly){
		if(filePath==null||"".equals(filePath)||propertiesMap==null||propertiesMap.size()==0)
			return ;
		if(!isUtterly){
//			platPath = Platform.getInstallLocation().getURL().getPath();
			initPlatPath();
			filePath = platPath+filePath;
		}
		Iterator iter = propertiesMap.entrySet().iterator(); 
		try {
			Properties props = new Properties();
			OutputStream os = new FileOutputStream(filePath);
			while (iter.hasNext()) { 
			    Map.Entry entry = (Map.Entry) iter.next(); 
			    String key = (String) entry.getKey(); 
			    String value = (String) entry.getValue();
			    props.setProperty(key, value); //$NON-NLS-1$
			} 
			props.store(os,""); //$NON-NLS-1$
			os.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void writeValuesByMapToFile(String filePath,HashMap<String, String> propertiesMap,boolean isUtterly){
		if(filePath==null||"".equals(filePath)||propertiesMap==null||propertiesMap.size()==0)
			return ;
		if(!isUtterly){
//			platPath = Platform.getInstallLocation().getURL().getPath();
			initPlatPath();
			filePath = platPath+filePath;
		}
		Iterator iter = propertiesMap.entrySet().iterator(); 
		try {

			FileOutputStream os = new FileOutputStream(filePath);
			while (iter.hasNext()) { 
			    Map.Entry entry = (Map.Entry) iter.next(); 
			    String key = (String) entry.getKey(); 
			    String value = (String) entry.getValue();
			    os.write((key+"="+value+"\r\n").getBytes("GBK"));
			} 
			os.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void writeValuesByMapToFileEnd(String filePath,HashMap<String, String> propertiesMap){
		if(filePath==null||"".equals(filePath)||propertiesMap==null||propertiesMap.size()==0)
			return ;
		Iterator iter = propertiesMap.entrySet().iterator(); 
		try {

			FileOutputStream os = new FileOutputStream(filePath,true);
			while (iter.hasNext()) { 
			    Map.Entry entry = (Map.Entry) iter.next(); 
			    String key = (String) entry.getKey(); 
			    String value = (String) entry.getValue();
			    os.write((key+"="+value+"\r\n").getBytes("GBK"));
			} 
			os.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	public static boolean checkFileIsExits(String filePath){
		File file = new File(filePath);
		return file.exists();
	}
	
	/**
	 * 根据参数取值
	 * @param filePath 属性文件路径
	 * @param filedNname
	 * @param isUtterly 是否绝路径
	 * @return
	 */
	public static String getValueByFiledName(String filePath,String filedNname,boolean isUtterly){
		String result = "";
		if(filedNname==null||"".equals(filedNname)||filePath==null||"".equals(filePath))
			return result;
		if(!isUtterly){
			initPlatPath();
//			platPath = Platform.getInstallLocation().getURL().getPath();
			filePath = platPath+filePath;
		}
		try {
			InputStream is = new FileInputStream(filePath);
			Properties props = new Properties();
			props.load(is);
			is.close();
			result = props.getProperty(filedNname);
//			props.clone();
		} catch (Exception e) {
			showExceptionMsg(e);
			return result;
		}
		return  result;
	}
	/**
	 * 根据参数数组取属性文件值Map
	 * @param filePath  属性文件路径
	 * @param filedNnameList 参数数组
	 * @param isUtterly 是否绝路径
	 * @return
	 */
	public static HashMap<String, String> getValueMapByFiledName(String filePath,List<String> filedNnameList,boolean isUtterly){
		if(filedNnameList==null||filedNnameList.size()==0||filePath==null||"".equals(filePath)){
			return null;
		}
		if(!isUtterly){
//			platPath = Platform.getInstallLocation().getURL().getPath();
			initPlatPath();
			filePath = platPath+filePath;
		}
		HashMap<String, String> resultMap = new HashMap<String, String>();
		try {
			InputStream is = new FileInputStream(filePath);
			Properties props = new Properties();
			props.load(is);
			is.close();
			for(String filedNname:filedNnameList){
				String value = props.getProperty(filedNname);
				if(value!=null){
					resultMap.put(filedNname, value);
				}
			}
		} catch (Exception e) {
			showExceptionMsg(e);
			return null;
		}
		return resultMap;
	}

	/**
	 * 显示例外信息
	 * 
	 * @param e
	 */
	public static void showExceptionMsg(Exception e, String s) {
		if (s == null || s.trim().equals("")) {
			s = e.getLocalizedMessage();
		}
		if (e.getMessage() != null && s.trim().equals(e.getMessage().trim())) {
			MessageDialog.openError(null, "系统错误", s);
		} else {
			MessageDialog.openError(null, "系统错误", s + "\n  "
					+ (e.getMessage() == null ? "" : e.getMessage()));
		}
	}

	/**
	 * 显示例外信息
	 * 
	 * @param e
	 */
	public static void showExceptionMsg(Exception e) {
		showExceptionMsg(e, "");
	}
	
	public static void main(String[] args) {
		List<String> list = new ArrayList<String>();
		list.add("230330");
//		ReadPropertyUtil.writeValuesByMap("/F:/eclipse/passtemp/PassResult.ini", ReadPropertyUtil.getValueMapByFiledName("/F:/eclipse/passtemp/PassResult.ini", list, true), true);
//		System.out.println(ReadPropertyUtil.getValueMapByFiledName("/F:/eclipse/passtemp/PassResult.ini", list, true));
//		System.out.println(ReadPropertyUtil.getValueByFiledName("/F:/eclipse/passtemp/PassResult.ini", "230330", true));
		HashMap<String,String> aaa = new HashMap<String,String>();
		aaa.put("userID", "123132");
		aaa.put("userID11", "123132");
		writeValuesByMapToFile("c:/asd.ini", aaa, true);
	}
}
